const puppeteer = require('puppeteer');
const newPage = require('./openNewTabData')
const gopPage = require('./goPage')
const utils = require('./utils')
require('./db')

let prePage = 1
let prCur = 0


// async function test() {
//   let doc = await jjModel.find()
//   console.log(doc)
// }
// test()


async function  main() { 
  const browser = await puppeteer.launch({headless: true}); // default is true
  const page = await browser.newPage();
  try {
    await page.goto('http://fund.eastmoney.com/data/fundranking.html#tall;c0;r;s3nzf;pn50;ddesc;qsd20201028;qed20211028;qdii;zq;gg;gzbd;gzfs;bbzt;sfbb');
  } catch (error) {
    console.log('超时了', error)
  }
  // 股票型
  // http://fund.eastmoney.com/data/fundranking.html#tgp;c0;r;s3nzf;pn50;ddesc;qsd20200821;qed20210821;qdii;zq;gg;gzbd;gzfs;bbzt;sfbb
  // 混合型
  // http://fund.eastmoney.com/data/fundranking.html#thh;c0;r;s3nzf;pn50;ddesc;qsd20200821;qed20210821;qdii;zq;gg;gzbd;gzfs;bbzt;sfbb

  


  // 获取页面 数量
  // let pageNum = await page.$eval('#pagebar', node=>{
  //   let ll = node.querySelectorAll('label')
  //   return parseInt(ll[ll.length-2].innerText)
  // })
  let pageNum = 100;

  if(prePage&&prePage!==1) {
    await gopPage(page, ''+prePage)
    let ele = await page.$$("#dbtable tbody tr") 
    let len = ele.length
    for(let j=prCur; j<len; j++) {
      console.log(`读取页面${prePage},第${j}条`)
      await newPage.openNewTab(browser, ele, j)
      // await utils.mock(500)
      await ele[j].dispose()
    }   
    prePage++
  }
  
  for(let i=prePage;i<=pageNum;i++) {
    console.log('读取页面'+i)
    if(i==1) {

    }else{
      let curpage = ''+i
      await gopPage(page, curpage)
    }
    let ele = await page.$$("#dbtable tbody tr") 
    let len = ele.length
    for(let j=0; j<len; j++) {
      console.log(`读取页面${i},第${j}条`)
      // if(j==0) {
      //   await newPage.openNewTab(browser, ele, j)
      // }
      try {
        await newPage.openNewTab(browser, ele, j)
        // await utils.mock(500)
        await ele[j].dispose()
      } catch (error) {
        console.log('跳转新页面出错', error)
      }
    }    
  }
}

main()